package com.mldong.controller;

import com.mldong.base.CommonResult;
import com.mldong.dto.LoginParam;
import com.mldong.vo.LoginUserVO;
import com.mldong.vo.TokenVO;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户控制层
 */
@RestController
public class UserController {

    @GetMapping("/hello")
    public CommonResult<?> hello() {
        String version = System.getenv("VERSION");
        Map<String,Object> map = new HashMap<>();
        map.put("version",version);
       return CommonResult.success(map);
    }
    /**
     * 用户登录
     * @param loginParam
     * @return
     */
    @PostMapping("/user/login")
    public CommonResult<?> userLogin(@RequestBody LoginParam loginParam) {
        if("admin".equals(loginParam.getUserName()) && "123456".equals(loginParam.getPassword())) {
            return CommonResult.success(TokenVO.builder().token("666666").build());
        }
        return CommonResult.fail("用户名或密码错误");
    }

    /**
     * 获取当前用户信息
     * @param token
     * @return
     */
    @PostMapping("/user/info")
    public CommonResult<LoginUserVO> userInfo(@RequestHeader(value = "token") String token) {
        if("666666".equals(token)) {
            return CommonResult.success(LoginUserVO.builder().id("1").userName("admin").realName("张三").userType(1).build());
        }
        return CommonResult.fail(99990403,"token不存在或已过期");
    }
}
